//
//  HomeDataViewController.m
//  Feel_at_home
//
//  Created by dllo on 15/9/28.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "HomeDataViewController.h"
#import "HomeDataView.h"
#import <HTTPTOOL.h>
#import "HomeModel.h"
#import <UIImageView+WebCache.h>
#import "TFHpple.h"
#import "TFHppleElement.h"
#import "XPathQuery.h"
@interface HomeDataViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *HeadArr;

@property (nonatomic,strong) NSMutableDictionary *HearDic;

@property (nonatomic,strong) NSString *cover_image_urlStr;
@property (nonatomic,strong) NSString *titleStr;
@property (nonatomic,strong) NSString *share_msgStr;

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation HomeDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.idstr = self.block1(self.str1);
    NSLog(@"9999 = %@",self.idstr);
    
    [self tabSubview];
    [self HandData];
//    [self webSubview];
    [self HandData1];
    
}
-(void)webSubview
{
    NSString *Url =[NSString stringWithFormat:@"http://bohejiaju.liwushuo.com/posts/%@",self.idstr];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:Url]];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
   
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,-60, self.view.frame.size.width  , self.view.frame.size.height+160)];
    //    self.webView.scalesPageToFit = NO;
    self.webView.scrollView.bounces = NO;
    
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
}

- (void)tabSubview
{
    self.tableview = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.tableview.separatorStyle = NO;
    self.tableview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableview];
}

- (void)HandData
{
    NSString *Url =[NSString stringWithFormat:@"http://api.bohejiaju.com/v1/posts/%@",self.idstr];
    
    [HTTPTOOL GETWithURL:Url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.cover_image_urlStr = [NSString string];
        self.titleStr = [NSString string];
        self.share_msgStr = [NSString string];
        
        self.HeadArr = [NSMutableArray array];
        self.HearDic = [NSMutableDictionary dictionary];
        self.HearDic = [result objectForKey:@"data"];
        
        HomeModel *home = [[HomeModel alloc]init];
        [home setValuesForKeysWithDictionary:self.HearDic];
        [self.HeadArr addObject:home];
        
        self.cover_image_urlStr = home.cover_image_url;
        self.titleStr = home.title;
        self.share_msgStr = home.share_msg;
        
        [self.tableview reloadData];
       
    } fail:^(NSError *error) {
        
        
    }];

    
}

- (void)HandData1
{
    //获取html的NSData数据
    NSString *Url =[NSString stringWithFormat:@"http://api.bohejiaju.com/v1/posts/%@",self.idstr];
    
    NSString *dataString = [NSString stringWithContentsOfURL:[NSURL URLWithString:Url] encoding:NSUTF8StringEncoding error:nil];  //htmlString是html网页的地址
    NSData *htmlData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    //    NSData *htmlData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:Url]];
    // 解析HTML数据
    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
    
    //根据标签来进行过滤
    NSArray *dataArray = [xpathParser searchWithXPathQuery:@"//img"];//这个数组中就有需要的值
    //开始整理数据
    for (TFHppleElement *hppleElement in dataArray) {
        //        //打印出该节点所有内容(包括标签)
        //        NSLog(@"777 == %@",hppleElement.raw);
     
            
            //打印出该节点所有内容(不包括标签)
//            NSLog(@"888 == %@",hppleElement.text);
        
            //过滤制定的数据
                    //if ([hppleElement objectForKey:@"img"]) {
        
        NSDictionary *dic = hppleElement.attributes;
        
        NSLog(@"88888 == %@",[dic objectForKey:@"src"]);
                    //}
        
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 500;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
            
        HomeDataView *view = [[HomeDataView alloc]init];
        view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
          NSLog(@"88888888888 == %@",self.cover_image_urlStr);
        
        [view.cover_image_url sd_setImageWithURL:[NSURL URLWithString:self.cover_image_urlStr] placeholderImage:[UIImage imageNamed:@"1"]];
     view.title.text = self.titleStr;
        
     view.share_msg.text = self.share_msgStr;
     
        
        [self.tableview addSubview:view];
            
        return view;
    }else{
    return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
